package com.lanou.controller;

import com.alibaba.fastjson.JSON;
import com.lanou.utils.JsonResult;
import com.lanou.utils.StringUtils;
import com.lanou.utils.VerifyCodeUtils;
import com.lanou.utils.code.VerifyCode;
import com.lanou.utils.login.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName LoginController
 * @Description: 虚拟登录
 * @Author 郅占伟
 * @Date 2023年10月30日
 * @Version: 1.0
 */
@Api(value = "登录",tags = "登录")
@RestController
public class LoginController {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

//    @Autowired
//    private LoginService
    @ApiOperation(value = "登录实现",httpMethod = "POST")
    @RequestMapping(value = "/login",produces = "text/html;charset=utf-8")
    public String login(@RequestBody LoginUser loginUser){
        // 判断用户名和密码是否为空 null ""
        if (StringUtils.isEmpty(loginUser.getUsername())){
            return JSON.toJSONString(new JsonResult(400,"用户名为空","用户名为空"));
        }
        if (StringUtils.isEmpty(loginUser.getPassword())){
            return JSON.toJSONString(new JsonResult(400,"密码错误","密码错误"));
        }

        String code = VerifyCode.getInstance().getCode();
        System.out.println(loginUser.getUuid());
        //String code = redisTemplate.opsForValue().get(loginUser.getUuid());
        System.out.println(code);

        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(loginUser.getLoginCode()) || !code.equals(loginUser.getLoginCode())){
            return JSON.toJSONString(new JsonResult(400,"验证码错误","验证码错误"));
        }

        //return true ? new JsonResult<>(200,"登陆成功") : new JsonResult(400,"登陆失败","登陆失败");

        return JSON.toJSONString(new JsonResult<>(200,"登陆成功"));
    }

}
